package com.hlyj.http.base.tool.lib_hlyj_base.utils;

import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.mmkv.MMKV;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class BaseModuleSpUtils {
    private volatile static BaseModuleSpUtils mInstance;
    private SharedPreferences mPref = MMKV.defaultMMKV();
    private Gson gson = new Gson(); // 新增 Gson 实例
    private BaseModuleSpUtils(){

    }

    /**
     *          单例模式
     * @return
     */
    public static BaseModuleSpUtils getInstance(){
        if (mInstance == null){
            synchronized (BaseModuleSpUtils.class){
                if (mInstance == null){
                    mInstance = new BaseModuleSpUtils();
                }
            }
        }
        return mInstance;
    }

    // ===================== 新增方法：List<String> 存储 =====================
    /**
     * 存储 List<String>
     */
    public void putStringList(String key, List<String> list) {
        String json = gson.toJson(list);
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(key, json);
        editor.commit();
    }

    /**
     * 清除指定键对应的 List<String>
     * @param key
     */
    public void removeStringList(String key) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.remove(key); // 删除特定键
        editor.commit(); // 或 editor.apply()
    }
    /**
     * 获取 List<String>（默认返回空列表）
     */
    public List<String> getStringList(String key) {
        String json = mPref.getString(key, null);
        if (json == null || json.isEmpty()) {
            return new ArrayList<>();
        }
        Type type = new TypeToken<List<String>>() {}.getType();
        return gson.fromJson(json, type);
    }
    public void putString(String key,String value){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(key,value);
        editor.commit();
    }

    public void putLong(String key,long value){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putLong(key,value);
        editor.commit();
    }
    public void putInt(String key,int value){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(key,value);
        editor.commit();
    }

    public void putFloat(String key,float value){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putFloat(key,value);
        editor.commit();
    }

    public void putBoolean(String key,boolean value){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(key,value);
        editor.commit();
    }

    public String getString(String key){
        return mPref.getString(key,"");
    }

    public String getString(String key,String value){
        return mPref.getString(key,value);
    }

    public long getLong(String key){
        return mPref.getLong(key,0);
    }

    public long getLong(String key,long value){
        return mPref.getLong(key,value);
    }

    public int getInt(String key){
        return mPref.getInt(key,0);
    }

    public int getInt(String key,int value){
        return mPref.getInt(key,value);
    }

    public float getFloat(String key){
        return mPref.getFloat(key,0);
    }

    public float getFloat(String key,float value){
        return mPref.getFloat(key,value);
    }

    public Boolean getBoolean(String key){
        return mPref.getBoolean(key,false);
    }

    public Boolean getBoolean(String key,boolean value){
        return mPref.getBoolean(key,value);
    }

    /**
     *          根据key清除value的缓存
     * @param key
     */
    public void remove(String key){
        SharedPreferences.Editor editor = mPref.edit();
        editor.remove(key);
        editor.commit();
    }
    /**
     *          清除所有数据缓存
     */
    public void clear(){
        SharedPreferences.Editor editor = mPref.edit();
        editor.clear();
        editor.commit();
    }
}
